package com.aaa.web.service.impl.webnoticeserviceimpl;

import com.aaa.entity.Notice;
import com.aaa.phone.systemconst.ISystemConst;
import com.aaa.util.EmpUtil;
import com.aaa.util.PictureUtil;
import com.aaa.web.commons.NoticeQuery;
import com.aaa.web.commons.Result;
import com.aaa.web.dao.webnoticedao.WebNoticeDao;
import com.aaa.web.service.noticeservice.WebNoticeService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @description: 后台公告业务实现层
 * @author: 王林林
 * @create: 2018/12/20 15:55
 **/
@Service
public class WebNoticeServiceImpl implements WebNoticeService {

    @Autowired
    private WebNoticeDao dao;
    /**获取所有公告或按条件查询公告*/
    @Override
    public Map getAllNotice(NoticeQuery query) {
        //自动分页
        int page = query.getPage();
        int size = query.getLimit();
        PageHelper.startPage(page,size);
        Map<String, Object> map = new HashMap<>();
        List<Map> allNotice = dao.getAllNotice(query);
        map.put("data",allNotice);
        //获取公告的数量
        map.put("count",dao.getAllNoticeCount(query));
        map.put("code",ISystemConst.ZERO);
        map.put("msg","");
        return map;
    }
    /**根据当前的noticeId删除广告**/
    @Override
    public Result delNoticeById(Integer noticeId) {
        int i = dao.delNoticeById(noticeId);
        Result result = new Result();
        if(i> ISystemConst.ZERO){
            result.setCode(ISystemConst.PHONE_SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.PHONE_DEFEAT_CODE);
        }
        return  result;
    }
    /**添加或修改广告信息*/
    @Override
    public Result addOrUpdateNotice(Notice notice) {
        Integer employeeid = EmpUtil.getEmp().getEmployeeid();
        notice.setEmployeeid(employeeid+"");
        int i;
        //根据notice_id是否有值判断是添加还是修改
        if(notice.getNoticeId()!=null){
           // System.out.println("dao + update");
             i=dao.updateNotice(notice);
        }else{
            //System.out.println("dao + add");
             i= dao.addNotice(notice);
        }
        Result result = new Result();
        if(i>ISystemConst.ZERO){
            result.setCode(ISystemConst.PHONE_SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.PHONE_DEFEAT_CODE);
        }
        return  result;
    }

    /**将图片上传到服务器*/
    @Override
    public Map uploadPhoto(MultipartFile file) {
        File f = null;
        try {
            f=File.createTempFile("tmp", null);
            file.transferTo(f);
            f.deleteOnExit();
            String imgurl = PictureUtil.getPicture(f);
            //System.out.println("success---"+imgurl);
            Map<String, Object> map = new HashMap<>();
            map.put("data",imgurl);
            map.put("code",ISystemConst.ZERO);
            map.put("msg","");
            return  map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
